var pool = require('../../config/mysql.connection');
var mapping = require('./mapping/index.mapping');

module.exports = {
	getImgByPlace:function(param , callback){
		pool.getConnection(function(err,connection){
			connection.query(mapping.getImgByPlace,[param.place],function(err , result){
				if(err){
					throw err;
				}

				callback(result);

				connection.release();
			});
		});
	},
	getHomeMenu:function(param , callback){
		pool.getConnection(function(err,connection){
			connection.query(mapping.getMenuGroup,function(err,groupResult){
				if(err){
					throw err;
				}
				var groupMenuObj = {};
				for(var r in groupResult){
					var grT = groupResult[r];
					groupMenuObj['m'+grT.ID] = grT;
					groupMenuObj['m'+grT.ID].menu=[];
				}
				connection.query(mapping.getHomeMenu,[param.place,param.roleId],function(err , result){
					if(err){
						throw err;
					}
					var groupMenu = {};
					for(var i in result){
						var menuObj = result[i];
						var gmJ = groupMenuObj['m'+menuObj.GROUP_ID];
						gmJ.menu.push(menuObj);
					}
					callback(groupMenuObj);
					connection.release();
				});
			});
		});
	}
};